﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;

namespace HZ.Supervise.Web.Secondphase.ReportCompany
{
    public partial class ReportOfCompanyFirstJY : BasePage
    {
        System.Data.DataTable dt;
        protected string HospitalTotal, DrugsTotal, FactoryTotal, DistributionTotal, OrderAmountTotal;
        protected string HospitalTotal_PS, DrugsTotal_PS, FactoryTotal_PS, DistributionTotal_PS, DistributionAmountTotal, StorageAmountTotal;
        string strcompanyname = "";
        string order = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            strcompanyname = String.IsNullOrEmpty(Request.QueryString["psname"]) ? "" : Request.QueryString["psname"].ToString();
            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);

                Databind();
                GetStatistics();
                BindChart();
            }
        }
        /// <summary>
        /// 图表绑定 
        /// </summary>
        private void BindChart()
        {

            //空为初始页
            if (dt.Rows.Count > 0)
            {
                string monch = "";//月份
                int count = 2;//次数
                string sort = "0";//属性 0:基药 1:非基药

                #region 图

                //***************** 基本药品*****************/
                Series series1 = new Series("基本药品经营企业采购总数量");
                series1.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series1.BorderWidth = 1;
                series1.ShadowOffset = 1;

                //***************** 非基本药品*******************/
                Series series2 = new Series("非基本药品经营企业采购总数量");
                series2.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series2.BorderWidth = 1;
                series2.ShadowOffset = 1;

                //***********************基本药品数量******************/
                Series series3 = new Series("基本药品经营企业入库总数量");
                series3.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series3.BorderWidth = 1;
                series3.ShadowOffset = 1;

                //***********************非基本药品数量******************/
                Series series4 = new Series("非基本药品经营企业入库总数量");
                series4.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series4.BorderWidth = 1;
                series4.ShadowOffset = 1;

                //***************** 基本药品金额 柱状  ****************/
                Series series5 = new Series("基本药品经营企业出库数量");
                series5.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series5.BorderWidth = 1;
                series5.ShadowOffset = 1;


                //***************** 非基本药品金额 柱状  **************/
                Series series6 = new Series("非基本药品经营企业出库数量");
                series6.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series6.BorderWidth = 1;
                series6.ShadowOffset = 1;

                //***************** 基本药品数量 曲线******************/
                Series series7 = new Series("基本药品经营企业库存数量");
                series7.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series7.BorderWidth = 1;
                series7.ShadowOffset = 1;

                //***************** 非基本药品数量 曲线****************/
                Series series8 = new Series("非基本药品经营企业库存数量");
                series8.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series8.BorderWidth = 1;
                series8.ShadowOffset = 1;
                #endregion

                #region 绑定值
                if (dt.Rows[0]["Source"].ToString().Equals("1"))
                {
                    count = 1;
                    monch = dt.Rows[0]["motch"].ToString();
                }
                for (int i = 0; i < dt.Rows.Count; i++) //添加数据
                {
                    if (dt.Rows[i]["motch"].ToString().Equals(monch))
                    {
                        if (i == 0)
                        {
                            series1.Points.AddXY(monch.Substring(4) + "月", 0);
                            series3.Points.AddXY(monch.Substring(4) + "月", 0);
                            series5.Points.AddXY(monch.Substring(4) + "月", 0);
                            series7.Points.AddXY(monch.Substring(4) + "月", 0);
                        }
                        series2.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PuchaseCountSum"].ToString())).ToString());
                        series4.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["WareHouseCount"].ToString())).ToString());
                        series6.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PuchaseCountSumHospital"].ToString())).ToString());
                        series8.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["StockCount"].ToString())).ToString());
                        count++;
                        sort = "1";

                    }
                    else
                    {
                        if (count == 2 && dt.Rows[i]["Source"].ToString().Equals("0"))
                        {
                            series1.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PuchaseCountSum"].ToString())).ToString());
                            series3.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["WareHouseCount"].ToString())).ToString());
                            series5.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PuchaseCountSumHospital"].ToString())).ToString());
                            series7.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["StockCount"].ToString())).ToString());
                            count = 1;
                            sort = "0";
                            monch = dt.Rows[i]["motch"].ToString();
                        }
                        else
                        {
                            if (sort.Equals("0"))
                            {
                                series2.Points.AddXY(monch.Substring(4) + "月", 0);
                                series4.Points.AddXY(monch.Substring(4) + "月", 0);
                                series6.Points.AddXY(monch.Substring(4) + "月", 0);
                                series8.Points.AddXY(monch.Substring(4) + "月", 0);

                            }
                            else
                            {
                                series1.Points.AddXY(monch.Substring(4) + "月", 0);
                                series3.Points.AddXY(monch.Substring(4) + "月", 0);
                                series5.Points.AddXY(monch.Substring(4) + "月", 0);
                                series7.Points.AddXY(monch.Substring(4) + "月", 0);
                            }

                            if (dt.Rows[i]["Source"].ToString().Equals("0"))
                            {
                                series1.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PuchaseCountSum"].ToString())).ToString());
                                series3.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["WareHouseCount"].ToString())).ToString());
                                series5.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PuchaseCountSumHospital"].ToString())).ToString());
                                series7.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["StockCount"].ToString())).ToString());
                                monch = dt.Rows[i]["motch"].ToString();
                                count = 1;
                                sort = "0";
                            }
                            else
                            {
                                series2.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PuchaseCountSum"].ToString())).ToString());
                                series4.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["WareHouseCount"].ToString())).ToString());
                                series6.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PuchaseCountSumHospital"].ToString())).ToString());
                                series8.Points.AddXY(dt.Rows[i]["motch"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["StockCount"].ToString())).ToString());
                                count = 2;
                                sort = "1";

                            }

                        }

                    }
                }
                #endregion

                #region 数据绑定到图

                series1.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series1);
                ChartPurchasing.Series["基本药品经营企业采购总数量"].ChartArea = "ChartComPurchasing";

                series2.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series2);
                ChartPurchasing.Series["非基本药品经营企业采购总数量"].ChartArea = "ChartComPurchasing";

                series3.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series3);
                ChartPurchasing.Series["基本药品经营企业入库总数量"].ChartArea = "ChartComPurchasing";

                series4.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series4);
                ChartPurchasing.Series["非基本药品经营企业入库总数量"].ChartArea = "ChartComPurchasing";

                series5.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series5);
                ChartPurchasing.Series["基本药品经营企业出库数量"].ChartArea = "ChartComPurchasing";


                series6.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series6);
                ChartPurchasing.Series["非基本药品经营企业出库数量"].ChartArea = "ChartComPurchasing";

                series7.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series7);
                ChartPurchasing.Series["基本药品经营企业库存数量"].ChartArea = "ChartComPurchasing";

                series8.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series8);
                ChartPurchasing.Series["非基本药品经营企业库存数量"].ChartArea = "ChartComPurchasing";
                #endregion

                #region 图片属性
                ChartPurchasing.ChartAreas["ChartComPurchasing"].AxisX.IsMarginVisible = true;
                ChartPurchasing.ChartAreas["ChartComPurchasing"].Area3DStyle.Enable3D = false;
                ChartPurchasing.ChartAreas["ChartComPurchasing"].AxisX.MajorGrid.Interval = 1;

                ChartPurchasing.ChartAreas["ChartComPurchasing"].AxisX.LineWidth = 1;
                ChartPurchasing.ChartAreas["ChartComPurchasing"].AxisY.LineWidth = 1;
                ChartPurchasing.ChartAreas["ChartComPurchasing"].AxisY.Title = "数量";
                ChartPurchasing.ChartAreas["ChartComPurchasing"].AxisX.Title = "月份";
                #endregion

                #region 分组
                ChartPurchasing.Series["基本药品经营企业采购总数量"]["StackedGroupName"] = "Group1";
                ChartPurchasing.Series["非基本药品经营企业采购总数量"]["StackedGroupName"] = "Group1";
                ChartPurchasing.Series["基本药品经营企业入库总数量"]["StackedGroupName"] = "Group2";
                ChartPurchasing.Series["非基本药品经营企业入库总数量"]["StackedGroupName"] = "Group2";
                ChartPurchasing.Series["基本药品经营企业出库数量"]["StackedGroupName"] = "Group3";
                ChartPurchasing.Series["非基本药品经营企业出库数量"]["StackedGroupName"] = "Group3";
                ChartPurchasing.Series["基本药品经营企业库存数量"]["StackedGroupName"] = "Group4";
                ChartPurchasing.Series["非基本药品经营企业库存数量"]["StackedGroupName"] = "Group4";
                ChartPurchasing.ResetAutoValues();
                #endregion

                #region 绑定数据TB
                HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

                DataTable dtChart1 = ie.GetSuperviseChart1(null);

                if (dtChart1.Rows.Count > 0)
                {
                    if (dtChart1.Rows[0]["HospitalCount"] != DBNull.Value) { HospitalTotal = string.Format("{0}个", dtChart1.Rows[0]["HospitalCount"].ToString()); }
                    if (dtChart1.Rows[0]["ProcureCatalogCount"] != DBNull.Value) { DrugsTotal = string.Format("{0}个", dtChart1.Rows[0]["ProcureCatalogCount"].ToString()); }
                    if (dtChart1.Rows[0]["CompanySCCount"] != DBNull.Value) { FactoryTotal = string.Format("{0}个", dtChart1.Rows[0]["CompanySCCount"].ToString()); }
                    if (dtChart1.Rows[0]["CompanyPSCount"] != DBNull.Value) { DistributionTotal = string.Format("{0}个", dtChart1.Rows[0]["CompanyPSCount"].ToString()); }
                    
                }

                DataTable dtChart2 = ie.GetSuperviseChart2(null);

                if (dtChart2.Rows.Count > 0)
                {
                    if (dtChart2.Rows[0]["HospitalCount"] != DBNull.Value) { HospitalTotal_PS = string.Format("{0}个", dtChart2.Rows[0]["HospitalCount"].ToString()); }
                    if (dtChart2.Rows[0]["ProcureCatalogCount"] != DBNull.Value) { DrugsTotal_PS = string.Format("{0}个", dtChart2.Rows[0]["ProcureCatalogCount"].ToString()); }
                    if (dtChart2.Rows[0]["CompanySCCount"] != DBNull.Value) { FactoryTotal_PS = string.Format("{0}个", dtChart2.Rows[0]["CompanySCCount"].ToString()); }
                    if (dtChart2.Rows[0]["CompanyPSCount"] != DBNull.Value) { DistributionTotal_PS = string.Format("{0}个", dtChart2.Rows[0]["CompanyPSCount"].ToString()); }
                    

                }
                #endregion
            }
            else
            {
                this.Panel1.Visible = false;
                lblnull.Visible = true;
            }

        }



        void GetStatistics()
        {
            double wareHouseOutCountTotal = 0;//采购总数量为
            double PurchaseCountTotal = 0; //入库总数量为
            double wareHouseCountTotal = 0;//出库总金额为
            double wareHouseOutAmountTotal = 0;//出库总数量为
            double InventoryCountTotal = 0;//库存总数量
            for (int i = 0; i < gvwProcureCatalog.Rows.Count; i++)
            {

                PurchaseCountTotal += double.Parse(gvwProcureCatalog.Rows[i].Cells[2].Text);
                wareHouseCountTotal += double.Parse(gvwProcureCatalog.Rows[i].Cells[3].Text);
                wareHouseOutAmountTotal += double.Parse(gvwProcureCatalog.Rows[i].Cells[4].Text);
                InventoryCountTotal += double.Parse(gvwProcureCatalog.Rows[i].Cells[5].Text);
                wareHouseOutCountTotal += double.Parse(gvwProcureCatalog.Rows[i].Cells[6].Text);


            }
            this.lblCount.Text = ";采购总数量为：" + PurchaseCountTotal.ToString() + ";入库总数量为：" + wareHouseCountTotal.ToString() + ";出库总数量为" + wareHouseOutAmountTotal.ToString() + ";库存总数量为" + InventoryCountTotal.ToString() + "出库金额" + wareHouseOutCountTotal.ToString();
        }
        public void Databind()
        {

            int t = 0;
            HZ.Supervise.IBLL.IBll_CompanyOrderDetail ibcd = HZ.Supervise.BLLFactory.IBll_CompanyOrderDetail();
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
            dt = ie.FixAllMonth(ibcd.GetCompanySC_sort(4, "", order, 0, 1, out t), 2, 0, "motch");
            gvwProcureCatalog.DataSource = dt;
            gvwProcureCatalog.DataBind();
            for (int j = 0; j < gvwProcureCatalog.Rows.Count; j++)
            {
                //名称相同才能合并
                //防止超出索引
                if (j < (gvwProcureCatalog.Rows.Count - 1))
                {
                    if (gvwProcureCatalog.Rows[j].Cells[0].Text.Trim() == gvwProcureCatalog.Rows[j + 1].Cells[0].Text.Trim())
                    {
                        //合并头
                        gvwProcureCatalog.Rows[j].Cells[0].RowSpan = 2;
                        gvwProcureCatalog.Rows[j + 1].Cells[0].Visible = false;

                        //合并尾
                        gvwProcureCatalog.Rows[j].Cells[4].RowSpan = 2;
                        gvwProcureCatalog.Rows[j + 1].Cells[4].Visible = false;
                        j++;
                    }
                    else
                    {

                    }
                }

                //合并

            }
        }

        protected void gvwProcureCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "CompanyOfMonthJY")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = ViewState["query"] as SuperviseQuery;

                query.Mid = e.CommandArgument.ToString();

                Session[sessionid] = query;

                if (!string.IsNullOrEmpty(query.Mid))
                {
                    Response.Redirect(string.Format("CompanyOfMonthJY.aspx?sessionid={0}", sessionid));
                }
                else
                {

                }
            }
        }
    }
}
